
/*
    Software License

    Copyright (C) 2021-05-24  Xoronos

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, version 3.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

/*
    Liabilities

    The software is provided "AS IS" without any warranty of any kind, either expressed,
    implied, or statutory, including, but not limited to, any warranty that the software
    will conform to specifications, any implied warranties of merchantability, fitness
    for a particular purpose, and freedom from infringement, and any warranty that the
    documentation will conform to the software, or any warranty that the software will
    be error free.

    In no event shall Xoronos be liable for any damages, including, but not limited to,
    direct, indirect, special or consequential damages, arising out of, resulting from,
    or in any way connected with this software, whether or not based upon warranty,
    contract, tort, or otherwise, whether or not injury was sustained by persons or
    property or otherwise, and whether or not loss was sustained from, or arose out of
    the results of, or use of, the software or services provided hereunder.

    To request the provided software under a different license you can contact us at
    support@xoronos.com
*/

#ifndef XORON_REPORTS_UTIL_H
#define XORON_REPORTS_UTIL_H

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

#define XPRINT_ERR(Y,X) \
        if( xlog.err_en ) { fprintf(xlog.err_fp ,"[XRN-ERR %10d]: %s, time %ld, FILE: %s , LINE %d, FUNCTION: %s\n", Y, X, time(NULL), __FILE__,__LINE__,__FUNCTION__); }

#define XPRINT_WAR(X,Y) \
        if ( Y > 0x80000000 ) for( int war_idx = 12 ; war_idx < 31 ; war_idx ++ ) { if (((( xlog.war_shown >> war_idx ) & 0x00000001 ) == 0 ) && (( Y & (uint32_t)( 0x00000001 << war_idx )) >> war_idx == 1 ) ) { if( xlog.war_en ) { fprintf(xlog.war_fp ,"[XRN-WAR]: %s, time %ld, FILE: %s , LINE %d, FUNCTION: %s\n", X, time(NULL), __FILE__,__LINE__,__FUNCTION__); xlog.war_shown = xlog.war_shown | (uint32_t) 0x00000001 << war_idx ; } } }

#define XPRINT_NOT(X) \
        if( xlog.not_en ) { if ( X == 0 ) { fprintf(xlog.not_fp ,"{ \"message_type\" : \"info\" , \"time\" : \"%ld\" , \"function\" : \"%s\" , \"result\" : \"PASS\" }\n", time(NULL), __FUNCTION__ ); } else { fprintf(xlog.not_fp ,"{ \"message_type\" : \"info\" , \"time\" : \"%ld\" , \"function\" : \"%s\" , \"result\" : \"FAIL\" }\n", time(NULL), __FUNCTION__ ); } }

#define XPRINT_DEB() \
       if( xlog.deb_en ) { fprintf(xlog.deb_fp ,"[XRN-DEB]: seed %x time %ld %s\n", xlog.deb_seed, time(NULL), __FUNCTION__ ); }

void xrn_free( void *ptr, uint64_t size
 );

#define XRN_FREE(X) xrn_free((void *)X,(uint64_t)sizeof(*X))
#define XRN_FREEN(X,Y) xrn_free((void *)X,(uint64_t)Y)
#define XRN_FREE_XSTATE(X,Y) xrn_free((void *)X,sizeof(xnum_t)*((uint64_t)(Y)+1))

void xrn_crc64( uint8_t * buffer, int len, uint64_t * crcval
 );

void xrn_crc32( uint8_t * buffer, int len, uint32_t * crcval
 );

void xrn_crc16( uint8_t * buffer, int len, uint16_t * crcval
 );

void xrn_crc8( uint8_t * buffer, int len, uint8_t * crcval
 );

int xrn_feof( FILE * fp
 );

static const uint64_t CRC64_TABLE[] = {
    UINT64_C( 0x0000000000000000 ), UINT64_C( 0x7ad870c830358979 ),
    UINT64_C( 0xf5b0e190606b12f2 ), UINT64_C( 0x8f689158505e9b8b ),
    UINT64_C( 0xc038e5739841b68f ), UINT64_C( 0xbae095bba8743ff6 ),
    UINT64_C( 0x358804e3f82aa47d ), UINT64_C( 0x4f50742bc81f2d04 ),
    UINT64_C( 0xab28ecb46814fe75 ), UINT64_C( 0xd1f09c7c5821770c ),
    UINT64_C( 0x5e980d24087fec87 ), UINT64_C( 0x24407dec384a65fe ),
    UINT64_C( 0x6b1009c7f05548fa ), UINT64_C( 0x11c8790fc060c183 ),
    UINT64_C( 0x9ea0e857903e5a08 ), UINT64_C( 0xe478989fa00bd371 ),
    UINT64_C( 0x7d08ff3b88be6f81 ), UINT64_C( 0x07d08ff3b88be6f8 ),
    UINT64_C( 0x88b81eabe8d57d73 ), UINT64_C( 0xf2606e63d8e0f40a ),
    UINT64_C( 0xbd301a4810ffd90e ), UINT64_C( 0xc7e86a8020ca5077 ),
    UINT64_C( 0x4880fbd87094cbfc ), UINT64_C( 0x32588b1040a14285 ),
    UINT64_C( 0xd620138fe0aa91f4 ), UINT64_C( 0xacf86347d09f188d ),
    UINT64_C( 0x2390f21f80c18306 ), UINT64_C( 0x594882d7b0f40a7f ),
    UINT64_C( 0x1618f6fc78eb277b ), UINT64_C( 0x6cc0863448deae02 ),
    UINT64_C( 0xe3a8176c18803589 ), UINT64_C( 0x997067a428b5bcf0 ),
    UINT64_C( 0xfa11fe77117cdf02 ), UINT64_C( 0x80c98ebf2149567b ),
    UINT64_C( 0x0fa11fe77117cdf0 ), UINT64_C( 0x75796f2f41224489 ),
    UINT64_C( 0x3a291b04893d698d ), UINT64_C( 0x40f16bccb908e0f4 ),
    UINT64_C( 0xcf99fa94e9567b7f ), UINT64_C( 0xb5418a5cd963f206 ),
    UINT64_C( 0x513912c379682177 ), UINT64_C( 0x2be1620b495da80e ),
    UINT64_C( 0xa489f35319033385 ), UINT64_C( 0xde51839b2936bafc ),
    UINT64_C( 0x9101f7b0e12997f8 ), UINT64_C( 0xebd98778d11c1e81 ),
    UINT64_C( 0x64b116208142850a ), UINT64_C( 0x1e6966e8b1770c73 ),
    UINT64_C( 0x8719014c99c2b083 ), UINT64_C( 0xfdc17184a9f739fa ),
    UINT64_C( 0x72a9e0dcf9a9a271 ), UINT64_C( 0x08719014c99c2b08 ),
    UINT64_C( 0x4721e43f0183060c ), UINT64_C( 0x3df994f731b68f75 ),
    UINT64_C( 0xb29105af61e814fe ), UINT64_C( 0xc849756751dd9d87 ),
    UINT64_C( 0x2c31edf8f1d64ef6 ), UINT64_C( 0x56e99d30c1e3c78f ),
    UINT64_C( 0xd9810c6891bd5c04 ), UINT64_C( 0xa3597ca0a188d57d ),
    UINT64_C( 0xec09088b6997f879 ), UINT64_C( 0x96d1784359a27100 ),
    UINT64_C( 0x19b9e91b09fcea8b ), UINT64_C( 0x636199d339c963f2 ),
    UINT64_C( 0xdf7adabd7a6e2d6f ), UINT64_C( 0xa5a2aa754a5ba416 ),
    UINT64_C( 0x2aca3b2d1a053f9d ), UINT64_C( 0x50124be52a30b6e4 ),
    UINT64_C( 0x1f423fcee22f9be0 ), UINT64_C( 0x659a4f06d21a1299 ),
    UINT64_C( 0xeaf2de5e82448912 ), UINT64_C( 0x902aae96b271006b ),
    UINT64_C( 0x74523609127ad31a ), UINT64_C( 0x0e8a46c1224f5a63 ),
    UINT64_C( 0x81e2d7997211c1e8 ), UINT64_C( 0xfb3aa75142244891 ),
    UINT64_C( 0xb46ad37a8a3b6595 ), UINT64_C( 0xceb2a3b2ba0eecec ),
    UINT64_C( 0x41da32eaea507767 ), UINT64_C( 0x3b024222da65fe1e ),
    UINT64_C( 0xa2722586f2d042ee ), UINT64_C( 0xd8aa554ec2e5cb97 ),
    UINT64_C( 0x57c2c41692bb501c ), UINT64_C( 0x2d1ab4dea28ed965 ),
    UINT64_C( 0x624ac0f56a91f461 ), UINT64_C( 0x1892b03d5aa47d18 ),
    UINT64_C( 0x97fa21650afae693 ), UINT64_C( 0xed2251ad3acf6fea ),
    UINT64_C( 0x095ac9329ac4bc9b ), UINT64_C( 0x7382b9faaaf135e2 ),
    UINT64_C( 0xfcea28a2faafae69 ), UINT64_C( 0x8632586aca9a2710 ),
    UINT64_C( 0xc9622c4102850a14 ), UINT64_C( 0xb3ba5c8932b0836d ),
    UINT64_C( 0x3cd2cdd162ee18e6 ), UINT64_C( 0x460abd1952db919f ),
    UINT64_C( 0x256b24ca6b12f26d ), UINT64_C( 0x5fb354025b277b14 ),
    UINT64_C( 0xd0dbc55a0b79e09f ), UINT64_C( 0xaa03b5923b4c69e6 ),
    UINT64_C( 0xe553c1b9f35344e2 ), UINT64_C( 0x9f8bb171c366cd9b ),
    UINT64_C( 0x10e3202993385610 ), UINT64_C( 0x6a3b50e1a30ddf69 ),
    UINT64_C( 0x8e43c87e03060c18 ), UINT64_C( 0xf49bb8b633338561 ),
    UINT64_C( 0x7bf329ee636d1eea ), UINT64_C( 0x012b592653589793 ),
    UINT64_C( 0x4e7b2d0d9b47ba97 ), UINT64_C( 0x34a35dc5ab7233ee ),
    UINT64_C( 0xbbcbcc9dfb2ca865 ), UINT64_C( 0xc113bc55cb19211c ),
    UINT64_C( 0x5863dbf1e3ac9dec ), UINT64_C( 0x22bbab39d3991495 ),
    UINT64_C( 0xadd33a6183c78f1e ), UINT64_C( 0xd70b4aa9b3f20667 ),
    UINT64_C( 0x985b3e827bed2b63 ), UINT64_C( 0xe2834e4a4bd8a21a ),
    UINT64_C( 0x6debdf121b863991 ), UINT64_C( 0x1733afda2bb3b0e8 ),
    UINT64_C( 0xf34b37458bb86399 ), UINT64_C( 0x8993478dbb8deae0 ),
    UINT64_C( 0x06fbd6d5ebd3716b ), UINT64_C( 0x7c23a61ddbe6f812 ),
    UINT64_C( 0x3373d23613f9d516 ), UINT64_C( 0x49aba2fe23cc5c6f ),
    UINT64_C( 0xc6c333a67392c7e4 ), UINT64_C( 0xbc1b436e43a74e9d ),
    UINT64_C( 0x95ac9329ac4bc9b5 ), UINT64_C( 0xef74e3e19c7e40cc ),
    UINT64_C( 0x601c72b9cc20db47 ), UINT64_C( 0x1ac40271fc15523e ),
    UINT64_C( 0x5594765a340a7f3a ), UINT64_C( 0x2f4c0692043ff643 ),
    UINT64_C( 0xa02497ca54616dc8 ), UINT64_C( 0xdafce7026454e4b1 ),
    UINT64_C( 0x3e847f9dc45f37c0 ), UINT64_C( 0x445c0f55f46abeb9 ),
    UINT64_C( 0xcb349e0da4342532 ), UINT64_C( 0xb1eceec59401ac4b ),
    UINT64_C( 0xfebc9aee5c1e814f ), UINT64_C( 0x8464ea266c2b0836 ),
    UINT64_C( 0x0b0c7b7e3c7593bd ), UINT64_C( 0x71d40bb60c401ac4 ),
    UINT64_C( 0xe8a46c1224f5a634 ), UINT64_C( 0x927c1cda14c02f4d ),
    UINT64_C( 0x1d148d82449eb4c6 ), UINT64_C( 0x67ccfd4a74ab3dbf ),
    UINT64_C( 0x289c8961bcb410bb ), UINT64_C( 0x5244f9a98c8199c2 ),
    UINT64_C( 0xdd2c68f1dcdf0249 ), UINT64_C( 0xa7f41839ecea8b30 ),
    UINT64_C( 0x438c80a64ce15841 ), UINT64_C( 0x3954f06e7cd4d138 ),
    UINT64_C( 0xb63c61362c8a4ab3 ), UINT64_C( 0xcce411fe1cbfc3ca ),
    UINT64_C( 0x83b465d5d4a0eece ), UINT64_C( 0xf96c151de49567b7 ),
    UINT64_C( 0x76048445b4cbfc3c ), UINT64_C( 0x0cdcf48d84fe7545 ),
    UINT64_C( 0x6fbd6d5ebd3716b7 ), UINT64_C( 0x15651d968d029fce ),
    UINT64_C( 0x9a0d8ccedd5c0445 ), UINT64_C( 0xe0d5fc06ed698d3c ),
    UINT64_C( 0xaf85882d2576a038 ), UINT64_C( 0xd55df8e515432941 ),
    UINT64_C( 0x5a3569bd451db2ca ), UINT64_C( 0x20ed197575283bb3 ),
    UINT64_C( 0xc49581ead523e8c2 ), UINT64_C( 0xbe4df122e51661bb ),
    UINT64_C( 0x3125607ab548fa30 ), UINT64_C( 0x4bfd10b2857d7349 ),
    UINT64_C( 0x04ad64994d625e4d ), UINT64_C( 0x7e7514517d57d734 ),
    UINT64_C( 0xf11d85092d094cbf ), UINT64_C( 0x8bc5f5c11d3cc5c6 ),
    UINT64_C( 0x12b5926535897936 ), UINT64_C( 0x686de2ad05bcf04f ),
    UINT64_C( 0xe70573f555e26bc4 ), UINT64_C( 0x9ddd033d65d7e2bd ),
    UINT64_C( 0xd28d7716adc8cfb9 ), UINT64_C( 0xa85507de9dfd46c0 ),
    UINT64_C( 0x273d9686cda3dd4b ), UINT64_C( 0x5de5e64efd965432 ),
    UINT64_C( 0xb99d7ed15d9d8743 ), UINT64_C( 0xc3450e196da80e3a ),
    UINT64_C( 0x4c2d9f413df695b1 ), UINT64_C( 0x36f5ef890dc31cc8 ),
    UINT64_C( 0x79a59ba2c5dc31cc ), UINT64_C( 0x037deb6af5e9b8b5 ),
    UINT64_C( 0x8c157a32a5b7233e ), UINT64_C( 0xf6cd0afa9582aa47 ),
    UINT64_C( 0x4ad64994d625e4da ), UINT64_C( 0x300e395ce6106da3 ),
    UINT64_C( 0xbf66a804b64ef628 ), UINT64_C( 0xc5bed8cc867b7f51 ),
    UINT64_C( 0x8aeeace74e645255 ), UINT64_C( 0xf036dc2f7e51db2c ),
    UINT64_C( 0x7f5e4d772e0f40a7 ), UINT64_C( 0x05863dbf1e3ac9de ),
    UINT64_C( 0xe1fea520be311aaf ), UINT64_C( 0x9b26d5e88e0493d6 ),
    UINT64_C( 0x144e44b0de5a085d ), UINT64_C( 0x6e963478ee6f8124 ),
    UINT64_C( 0x21c640532670ac20 ), UINT64_C( 0x5b1e309b16452559 ),
    UINT64_C( 0xd476a1c3461bbed2 ), UINT64_C( 0xaeaed10b762e37ab ),
    UINT64_C( 0x37deb6af5e9b8b5b ), UINT64_C( 0x4d06c6676eae0222 ),
    UINT64_C( 0xc26e573f3ef099a9 ), UINT64_C( 0xb8b627f70ec510d0 ),
    UINT64_C( 0xf7e653dcc6da3dd4 ), UINT64_C( 0x8d3e2314f6efb4ad ),
    UINT64_C( 0x0256b24ca6b12f26 ), UINT64_C( 0x788ec2849684a65f ),
    UINT64_C( 0x9cf65a1b368f752e ), UINT64_C( 0xe62e2ad306bafc57 ),
    UINT64_C( 0x6946bb8b56e467dc ), UINT64_C( 0x139ecb4366d1eea5 ),
    UINT64_C( 0x5ccebf68aecec3a1 ), UINT64_C( 0x2616cfa09efb4ad8 ),
    UINT64_C( 0xa97e5ef8cea5d153 ), UINT64_C( 0xd3a62e30fe90582a ),
    UINT64_C( 0xb0c7b7e3c7593bd8 ), UINT64_C( 0xca1fc72bf76cb2a1 ),
    UINT64_C( 0x45775673a732292a ), UINT64_C( 0x3faf26bb9707a053 ),
    UINT64_C( 0x70ff52905f188d57 ), UINT64_C( 0x0a2722586f2d042e ),
    UINT64_C( 0x854fb3003f739fa5 ), UINT64_C( 0xff97c3c80f4616dc ),
    UINT64_C( 0x1bef5b57af4dc5ad ), UINT64_C( 0x61372b9f9f784cd4 ),
    UINT64_C( 0xee5fbac7cf26d75f ), UINT64_C( 0x9487ca0fff135e26 ),
    UINT64_C( 0xdbd7be24370c7322 ), UINT64_C( 0xa10fceec0739fa5b ),
    UINT64_C( 0x2e675fb4576761d0 ), UINT64_C( 0x54bf2f7c6752e8a9 ),
    UINT64_C( 0xcdcf48d84fe75459 ), UINT64_C( 0xb71738107fd2dd20 ),
    UINT64_C( 0x387fa9482f8c46ab ), UINT64_C( 0x42a7d9801fb9cfd2 ),
    UINT64_C( 0x0df7adabd7a6e2d6 ), UINT64_C( 0x772fdd63e7936baf ),
    UINT64_C( 0xf8474c3bb7cdf024 ), UINT64_C( 0x829f3cf387f8795d ),
    UINT64_C( 0x66e7a46c27f3aa2c ), UINT64_C( 0x1c3fd4a417c62355 ),
    UINT64_C( 0x935745fc4798b8de ), UINT64_C( 0xe98f353477ad31a7 ),
    UINT64_C( 0xa6df411fbfb21ca3 ), UINT64_C( 0xdc0731d78f8795da ),
    UINT64_C( 0x536fa08fdfd90e51 ), UINT64_C( 0x29b7d047efec8728 )
};

static const unsigned int CRC32_TABLE[] = {
    UINT32_C( 0x00000000 ), UINT32_C( 0x04c11db7 ), UINT32_C( 0x09823b6e ), UINT32_C( 0x0d4326d9 ),
    UINT32_C( 0x130476dc ), UINT32_C( 0x17c56b6b ), UINT32_C( 0x1a864db2 ), UINT32_C( 0x1e475005 ),
    UINT32_C( 0x2608edb8 ), UINT32_C( 0x22c9f00f ), UINT32_C( 0x2f8ad6d6 ), UINT32_C( 0x2b4bcb61 ),
    UINT32_C( 0x350c9b64 ), UINT32_C( 0x31cd86d3 ), UINT32_C( 0x3c8ea00a ), UINT32_C( 0x384fbdbd ),
    UINT32_C( 0x4c11db70 ), UINT32_C( 0x48d0c6c7 ), UINT32_C( 0x4593e01e ), UINT32_C( 0x4152fda9 ),
    UINT32_C( 0x5f15adac ), UINT32_C( 0x5bd4b01b ), UINT32_C( 0x569796c2 ), UINT32_C( 0x52568b75 ),
    UINT32_C( 0x6a1936c8 ), UINT32_C( 0x6ed82b7f ), UINT32_C( 0x639b0da6 ), UINT32_C( 0x675a1011 ),
    UINT32_C( 0x791d4014 ), UINT32_C( 0x7ddc5da3 ), UINT32_C( 0x709f7b7a ), UINT32_C( 0x745e66cd ),
    UINT32_C( 0x9823b6e0 ), UINT32_C( 0x9ce2ab57 ), UINT32_C( 0x91a18d8e ), UINT32_C( 0x95609039 ),
    UINT32_C( 0x8b27c03c ), UINT32_C( 0x8fe6dd8b ), UINT32_C( 0x82a5fb52 ), UINT32_C( 0x8664e6e5 ),
    UINT32_C( 0xbe2b5b58 ), UINT32_C( 0xbaea46ef ), UINT32_C( 0xb7a96036 ), UINT32_C( 0xb3687d81 ),
    UINT32_C( 0xad2f2d84 ), UINT32_C( 0xa9ee3033 ), UINT32_C( 0xa4ad16ea ), UINT32_C( 0xa06c0b5d ),
    UINT32_C( 0xd4326d90 ), UINT32_C( 0xd0f37027 ), UINT32_C( 0xddb056fe ), UINT32_C( 0xd9714b49 ),
    UINT32_C( 0xc7361b4c ), UINT32_C( 0xc3f706fb ), UINT32_C( 0xceb42022 ), UINT32_C( 0xca753d95 ),
    UINT32_C( 0xf23a8028 ), UINT32_C( 0xf6fb9d9f ), UINT32_C( 0xfbb8bb46 ), UINT32_C( 0xff79a6f1 ),
    UINT32_C( 0xe13ef6f4 ), UINT32_C( 0xe5ffeb43 ), UINT32_C( 0xe8bccd9a ), UINT32_C( 0xec7dd02d ),
    UINT32_C( 0x34867077 ), UINT32_C( 0x30476dc0 ), UINT32_C( 0x3d044b19 ), UINT32_C( 0x39c556ae ),
    UINT32_C( 0x278206ab ), UINT32_C( 0x23431b1c ), UINT32_C( 0x2e003dc5 ), UINT32_C( 0x2ac12072 ),
    UINT32_C( 0x128e9dcf ), UINT32_C( 0x164f8078 ), UINT32_C( 0x1b0ca6a1 ), UINT32_C( 0x1fcdbb16 ),
    UINT32_C( 0x018aeb13 ), UINT32_C( 0x054bf6a4 ), UINT32_C( 0x0808d07d ), UINT32_C( 0x0cc9cdca ),
    UINT32_C( 0x7897ab07 ), UINT32_C( 0x7c56b6b0 ), UINT32_C( 0x71159069 ), UINT32_C( 0x75d48dde ),
    UINT32_C( 0x6b93dddb ), UINT32_C( 0x6f52c06c ), UINT32_C( 0x6211e6b5 ), UINT32_C( 0x66d0fb02 ),
    UINT32_C( 0x5e9f46bf ), UINT32_C( 0x5a5e5b08 ), UINT32_C( 0x571d7dd1 ), UINT32_C( 0x53dc6066 ),
    UINT32_C( 0x4d9b3063 ), UINT32_C( 0x495a2dd4 ), UINT32_C( 0x44190b0d ), UINT32_C( 0x40d816ba ),
    UINT32_C( 0xaca5c697 ), UINT32_C( 0xa864db20 ), UINT32_C( 0xa527fdf9 ), UINT32_C( 0xa1e6e04e ),
    UINT32_C( 0xbfa1b04b ), UINT32_C( 0xbb60adfc ), UINT32_C( 0xb6238b25 ), UINT32_C( 0xb2e29692 ),
    UINT32_C( 0x8aad2b2f ), UINT32_C( 0x8e6c3698 ), UINT32_C( 0x832f1041 ), UINT32_C( 0x87ee0df6 ),
    UINT32_C( 0x99a95df3 ), UINT32_C( 0x9d684044 ), UINT32_C( 0x902b669d ), UINT32_C( 0x94ea7b2a ),
    UINT32_C( 0xe0b41de7 ), UINT32_C( 0xe4750050 ), UINT32_C( 0xe9362689 ), UINT32_C( 0xedf73b3e ),
    UINT32_C( 0xf3b06b3b ), UINT32_C( 0xf771768c ), UINT32_C( 0xfa325055 ), UINT32_C( 0xfef34de2 ),
    UINT32_C( 0xc6bcf05f ), UINT32_C( 0xc27dede8 ), UINT32_C( 0xcf3ecb31 ), UINT32_C( 0xcbffd686 ),
    UINT32_C( 0xd5b88683 ), UINT32_C( 0xd1799b34 ), UINT32_C( 0xdc3abded ), UINT32_C( 0xd8fba05a ),
    UINT32_C( 0x690ce0ee ), UINT32_C( 0x6dcdfd59 ), UINT32_C( 0x608edb80 ), UINT32_C( 0x644fc637 ),
    UINT32_C( 0x7a089632 ), UINT32_C( 0x7ec98b85 ), UINT32_C( 0x738aad5c ), UINT32_C( 0x774bb0eb ),
    UINT32_C( 0x4f040d56 ), UINT32_C( 0x4bc510e1 ), UINT32_C( 0x46863638 ), UINT32_C( 0x42472b8f ),
    UINT32_C( 0x5c007b8a ), UINT32_C( 0x58c1663d ), UINT32_C( 0x558240e4 ), UINT32_C( 0x51435d53 ),
    UINT32_C( 0x251d3b9e ), UINT32_C( 0x21dc2629 ), UINT32_C( 0x2c9f00f0 ), UINT32_C( 0x285e1d47 ),
    UINT32_C( 0x36194d42 ), UINT32_C( 0x32d850f5 ), UINT32_C( 0x3f9b762c ), UINT32_C( 0x3b5a6b9b ),
    UINT32_C( 0x0315d626 ), UINT32_C( 0x07d4cb91 ), UINT32_C( 0x0a97ed48 ), UINT32_C( 0x0e56f0ff ),
    UINT32_C( 0x1011a0fa ), UINT32_C( 0x14d0bd4d ), UINT32_C( 0x19939b94 ), UINT32_C( 0x1d528623 ),
    UINT32_C( 0xf12f560e ), UINT32_C( 0xf5ee4bb9 ), UINT32_C( 0xf8ad6d60 ), UINT32_C( 0xfc6c70d7 ),
    UINT32_C( 0xe22b20d2 ), UINT32_C( 0xe6ea3d65 ), UINT32_C( 0xeba91bbc ), UINT32_C( 0xef68060b ),
    UINT32_C( 0xd727bbb6 ), UINT32_C( 0xd3e6a601 ), UINT32_C( 0xdea580d8 ), UINT32_C( 0xda649d6f ),
    UINT32_C( 0xc423cd6a ), UINT32_C( 0xc0e2d0dd ), UINT32_C( 0xcda1f604 ), UINT32_C( 0xc960ebb3 ),
    UINT32_C( 0xbd3e8d7e ), UINT32_C( 0xb9ff90c9 ), UINT32_C( 0xb4bcb610 ), UINT32_C( 0xb07daba7 ),
    UINT32_C( 0xae3afba2 ), UINT32_C( 0xaafbe615 ), UINT32_C( 0xa7b8c0cc ), UINT32_C( 0xa379dd7b ),
    UINT32_C( 0x9b3660c6 ), UINT32_C( 0x9ff77d71 ), UINT32_C( 0x92b45ba8 ), UINT32_C( 0x9675461f ),
    UINT32_C( 0x8832161a ), UINT32_C( 0x8cf30bad ), UINT32_C( 0x81b02d74 ), UINT32_C( 0x857130c3 ),
    UINT32_C( 0x5d8a9099 ), UINT32_C( 0x594b8d2e ), UINT32_C( 0x5408abf7 ), UINT32_C( 0x50c9b640 ),
    UINT32_C( 0x4e8ee645 ), UINT32_C( 0x4a4ffbf2 ), UINT32_C( 0x470cdd2b ), UINT32_C( 0x43cdc09c ),
    UINT32_C( 0x7b827d21 ), UINT32_C( 0x7f436096 ), UINT32_C( 0x7200464f ), UINT32_C( 0x76c15bf8 ),
    UINT32_C( 0x68860bfd ), UINT32_C( 0x6c47164a ), UINT32_C( 0x61043093 ), UINT32_C( 0x65c52d24 ),
    UINT32_C( 0x119b4be9 ), UINT32_C( 0x155a565e ), UINT32_C( 0x18197087 ), UINT32_C( 0x1cd86d30 ),
    UINT32_C( 0x029f3d35 ), UINT32_C( 0x065e2082 ), UINT32_C( 0x0b1d065b ), UINT32_C( 0x0fdc1bec ),
    UINT32_C( 0x3793a651 ), UINT32_C( 0x3352bbe6 ), UINT32_C( 0x3e119d3f ), UINT32_C( 0x3ad08088 ),
    UINT32_C( 0x2497d08d ), UINT32_C( 0x2056cd3a ), UINT32_C( 0x2d15ebe3 ), UINT32_C( 0x29d4f654 ),
    UINT32_C( 0xc5a92679 ), UINT32_C( 0xc1683bce ), UINT32_C( 0xcc2b1d17 ), UINT32_C( 0xc8ea00a0 ),
    UINT32_C( 0xd6ad50a5 ), UINT32_C( 0xd26c4d12 ), UINT32_C( 0xdf2f6bcb ), UINT32_C( 0xdbee767c ),
    UINT32_C( 0xe3a1cbc1 ), UINT32_C( 0xe760d676 ), UINT32_C( 0xea23f0af ), UINT32_C( 0xeee2ed18 ),
    UINT32_C( 0xf0a5bd1d ), UINT32_C( 0xf464a0aa ), UINT32_C( 0xf9278673 ), UINT32_C( 0xfde69bc4 ),
    UINT32_C( 0x89b8fd09 ), UINT32_C( 0x8d79e0be ), UINT32_C( 0x803ac667 ), UINT32_C( 0x84fbdbd0 ),
    UINT32_C( 0x9abc8bd5 ), UINT32_C( 0x9e7d9662 ), UINT32_C( 0x933eb0bb ), UINT32_C( 0x97ffad0c ),
    UINT32_C( 0xafb010b1 ), UINT32_C( 0xab710d06 ), UINT32_C( 0xa6322bdf ), UINT32_C( 0xa2f33668 ),
    UINT32_C( 0xbcb4666d ), UINT32_C( 0xb8757bda ), UINT32_C( 0xb5365d03 ), UINT32_C( 0xb1f740b4 )
};

static const uint16_t CRC16_TABLE[] = {
    UINT16_C( 0x0000 ), UINT16_C( 0xc181 ), UINT16_C( 0xc301 ), UINT16_C( 0x0280 ), UINT16_C( 0xc241 ),
    UINT16_C( 0xc601 ), UINT16_C( 0x0780 ), UINT16_C( 0x0500 ), UINT16_C( 0xc481 ), UINT16_C( 0x0440 ),
    UINT16_C( 0xcc01 ), UINT16_C( 0x0d80 ), UINT16_C( 0x0f00 ), UINT16_C( 0xce81 ), UINT16_C( 0x0e40 ),
    UINT16_C( 0x0a00 ), UINT16_C( 0xcb81 ), UINT16_C( 0xc901 ), UINT16_C( 0x0880 ), UINT16_C( 0xc841 ),
    UINT16_C( 0xd801 ), UINT16_C( 0x1980 ), UINT16_C( 0x1b00 ), UINT16_C( 0xda81 ), UINT16_C( 0x1a40 ),
    UINT16_C( 0x1e00 ), UINT16_C( 0xdf81 ), UINT16_C( 0xdd01 ), UINT16_C( 0x1c80 ), UINT16_C( 0xdc41 ),
    UINT16_C( 0x1400 ), UINT16_C( 0xd581 ), UINT16_C( 0xd701 ), UINT16_C( 0x1680 ), UINT16_C( 0xd641 ),
    UINT16_C( 0xd201 ), UINT16_C( 0x1380 ), UINT16_C( 0x1100 ), UINT16_C( 0xd081 ), UINT16_C( 0x1040 ),
    UINT16_C( 0xf001 ), UINT16_C( 0x3180 ), UINT16_C( 0x3300 ), UINT16_C( 0xf281 ), UINT16_C( 0x3240 ),
    UINT16_C( 0x3600 ), UINT16_C( 0xf781 ), UINT16_C( 0xf501 ), UINT16_C( 0x3480 ), UINT16_C( 0xf441 ),
    UINT16_C( 0x3c00 ), UINT16_C( 0xfd81 ), UINT16_C( 0xff01 ), UINT16_C( 0x3e80 ), UINT16_C( 0xfe41 ),
    UINT16_C( 0xfa01 ), UINT16_C( 0x3b80 ), UINT16_C( 0x3900 ), UINT16_C( 0xf881 ), UINT16_C( 0x3840 ),
    UINT16_C( 0x2800 ), UINT16_C( 0xe981 ), UINT16_C( 0xeb01 ), UINT16_C( 0x2a80 ), UINT16_C( 0xea41 ),
    UINT16_C( 0xee01 ), UINT16_C( 0x2f80 ), UINT16_C( 0x2d00 ), UINT16_C( 0xec81 ), UINT16_C( 0x2c40 ),
    UINT16_C( 0xe401 ), UINT16_C( 0x2580 ), UINT16_C( 0x2700 ), UINT16_C( 0xe681 ), UINT16_C( 0x2640 ),
    UINT16_C( 0x2200 ), UINT16_C( 0xe381 ), UINT16_C( 0xe101 ), UINT16_C( 0x2080 ), UINT16_C( 0xe041 ),
    UINT16_C( 0xa001 ), UINT16_C( 0x6180 ), UINT16_C( 0x6300 ), UINT16_C( 0xa281 ), UINT16_C( 0x6240 ),
    UINT16_C( 0x6600 ), UINT16_C( 0xa781 ), UINT16_C( 0xa501 ), UINT16_C( 0x6480 ), UINT16_C( 0xa441 ),
    UINT16_C( 0x6c00 ), UINT16_C( 0xad81 ), UINT16_C( 0xaf01 ), UINT16_C( 0x6e80 ), UINT16_C( 0xae41 ),
    UINT16_C( 0xaa01 ), UINT16_C( 0x6b80 ), UINT16_C( 0x6900 ), UINT16_C( 0xa881 ), UINT16_C( 0x6840 ),
    UINT16_C( 0x7800 ), UINT16_C( 0xb981 ), UINT16_C( 0xbb01 ), UINT16_C( 0x7a80 ), UINT16_C( 0xba41 ),
    UINT16_C( 0xbe01 ), UINT16_C( 0x7f80 ), UINT16_C( 0x7d00 ), UINT16_C( 0xbc81 ), UINT16_C( 0x7c40 ),
    UINT16_C( 0xb401 ), UINT16_C( 0x7580 ), UINT16_C( 0x7700 ), UINT16_C( 0xb681 ), UINT16_C( 0x7640 ),
    UINT16_C( 0x7200 ), UINT16_C( 0xb381 ), UINT16_C( 0xb101 ), UINT16_C( 0x7080 ), UINT16_C( 0xb041 ),
    UINT16_C( 0x5000 ), UINT16_C( 0x9181 ), UINT16_C( 0x9301 ), UINT16_C( 0x5280 ), UINT16_C( 0x9241 ),
    UINT16_C( 0x9601 ), UINT16_C( 0x5780 ), UINT16_C( 0x5500 ), UINT16_C( 0x9481 ), UINT16_C( 0x5440 ),
    UINT16_C( 0x9c01 ), UINT16_C( 0x5d80 ), UINT16_C( 0x5f00 ), UINT16_C( 0x9e81 ), UINT16_C( 0x5e40 ),
    UINT16_C( 0x5a00 ), UINT16_C( 0x9b81 ), UINT16_C( 0x9901 ), UINT16_C( 0x5880 ), UINT16_C( 0x9841 ),
    UINT16_C( 0x8801 ), UINT16_C( 0x4980 ), UINT16_C( 0x4b00 ), UINT16_C( 0x8a81 ), UINT16_C( 0x4a40 ),
    UINT16_C( 0x4e00 ), UINT16_C( 0x8f81 ), UINT16_C( 0x8d01 ), UINT16_C( 0x4c80 ), UINT16_C( 0x8c41 ),
    UINT16_C( 0x4400 ), UINT16_C( 0x8581 ), UINT16_C( 0x8701 ), UINT16_C( 0x4680 ), UINT16_C( 0x8641 ),
    UINT16_C( 0x8201 ), UINT16_C( 0x4380 ), UINT16_C( 0x4100 ), UINT16_C( 0x8081 ), UINT16_C( 0x4040 )
};

static const uint8_t CRC8_TABLE[] = {
    UINT8_C( 0x00 ), UINT8_C( 0x07 ), UINT8_C( 0x0e ), UINT8_C( 0x09 ), UINT8_C( 0x1c ), UINT8_C( 0x1b ),
    UINT8_C( 0x12 ), UINT8_C( 0x15 ), UINT8_C( 0x38 ), UINT8_C( 0x3f ), UINT8_C( 0x36 ), UINT8_C( 0x31 ),
    UINT8_C( 0x24 ), UINT8_C( 0x23 ), UINT8_C( 0x2a ), UINT8_C( 0x2d ), UINT8_C( 0x70 ), UINT8_C( 0x77 ),
    UINT8_C( 0x7e ), UINT8_C( 0x79 ), UINT8_C( 0x6c ), UINT8_C( 0x6b ), UINT8_C( 0x62 ), UINT8_C( 0x65 ),
    UINT8_C( 0x48 ), UINT8_C( 0x4f ), UINT8_C( 0x46 ), UINT8_C( 0x41 ), UINT8_C( 0x54 ), UINT8_C( 0x53 ),
    UINT8_C( 0x5a ), UINT8_C( 0x5d ), UINT8_C( 0xe0 ), UINT8_C( 0xe7 ), UINT8_C( 0xee ), UINT8_C( 0xe9 ),
    UINT8_C( 0xfc ), UINT8_C( 0xfb ), UINT8_C( 0xf2 ), UINT8_C( 0xf5 ), UINT8_C( 0xd8 ), UINT8_C( 0xdf ),
    UINT8_C( 0xd6 ), UINT8_C( 0xd1 ), UINT8_C( 0xc4 ), UINT8_C( 0xc3 ), UINT8_C( 0xca ), UINT8_C( 0xcd ),
    UINT8_C( 0x90 ), UINT8_C( 0x97 ), UINT8_C( 0x9e ), UINT8_C( 0x99 ), UINT8_C( 0x8c ), UINT8_C( 0x8b ),
    UINT8_C( 0x82 ), UINT8_C( 0x85 ), UINT8_C( 0xa8 ), UINT8_C( 0xaf ), UINT8_C( 0xa6 ), UINT8_C( 0xa1 ),
    UINT8_C( 0xb4 ), UINT8_C( 0xb3 ), UINT8_C( 0xba ), UINT8_C( 0xbd ), UINT8_C( 0xc7 ), UINT8_C( 0xc0 ),
    UINT8_C( 0xc9 ), UINT8_C( 0xce ), UINT8_C( 0xdb ), UINT8_C( 0xdc ), UINT8_C( 0xd5 ), UINT8_C( 0xd2 ),
    UINT8_C( 0xff ), UINT8_C( 0xf8 ), UINT8_C( 0xf1 ), UINT8_C( 0xf6 ), UINT8_C( 0xe3 ), UINT8_C( 0xe4 ),
    UINT8_C( 0xed ), UINT8_C( 0xea ), UINT8_C( 0xb7 ), UINT8_C( 0xb0 ), UINT8_C( 0xb9 ), UINT8_C( 0xbe ),
    UINT8_C( 0xab ), UINT8_C( 0xac ), UINT8_C( 0xa5 ), UINT8_C( 0xa2 ), UINT8_C( 0x8f ), UINT8_C( 0x88 ),
    UINT8_C( 0x81 ), UINT8_C( 0x86 ), UINT8_C( 0x93 ), UINT8_C( 0x94 ), UINT8_C( 0x9d ), UINT8_C( 0x9a ),
    UINT8_C( 0x27 ), UINT8_C( 0x20 ), UINT8_C( 0x29 ), UINT8_C( 0x2e ), UINT8_C( 0x3b ), UINT8_C( 0x3c ),
    UINT8_C( 0x35 ), UINT8_C( 0x32 ), UINT8_C( 0x1f ), UINT8_C( 0x18 ), UINT8_C( 0x11 ), UINT8_C( 0x16 ),
    UINT8_C( 0x03 ), UINT8_C( 0x04 ), UINT8_C( 0x0d ), UINT8_C( 0x0a ), UINT8_C( 0x57 ), UINT8_C( 0x50 ),
    UINT8_C( 0x59 ), UINT8_C( 0x5e ), UINT8_C( 0x4b ), UINT8_C( 0x4c ), UINT8_C( 0x45 ), UINT8_C( 0x42 ),
    UINT8_C( 0x6f ), UINT8_C( 0x68 ), UINT8_C( 0x61 ), UINT8_C( 0x66 ), UINT8_C( 0x73 ), UINT8_C( 0x74 ),
    UINT8_C( 0x7d ), UINT8_C( 0x7a ), UINT8_C( 0x89 ), UINT8_C( 0x8e ), UINT8_C( 0x87 ), UINT8_C( 0x80 ),
    UINT8_C( 0x95 ), UINT8_C( 0x92 ), UINT8_C( 0x9b ), UINT8_C( 0x9c ), UINT8_C( 0xb1 ), UINT8_C( 0xb6 ),
    UINT8_C( 0xbf ), UINT8_C( 0xb8 ), UINT8_C( 0xad ), UINT8_C( 0xaa ), UINT8_C( 0xa3 ), UINT8_C( 0xa4 ),
    UINT8_C( 0xf9 ), UINT8_C( 0xfe ), UINT8_C( 0xf7 ), UINT8_C( 0xf0 ), UINT8_C( 0xe5 ), UINT8_C( 0xe2 ),
    UINT8_C( 0xeb ), UINT8_C( 0xec ), UINT8_C( 0xc1 ), UINT8_C( 0xc6 ), UINT8_C( 0xcf ), UINT8_C( 0xc8 ),
    UINT8_C( 0xdd ), UINT8_C( 0xda ), UINT8_C( 0xd3 ), UINT8_C( 0xd4 ), UINT8_C( 0x69 ), UINT8_C( 0x6e ),
    UINT8_C( 0x67 ), UINT8_C( 0x60 ), UINT8_C( 0x75 ), UINT8_C( 0x72 ), UINT8_C( 0x7b ), UINT8_C( 0x7c ),
    UINT8_C( 0x51 ), UINT8_C( 0x56 ), UINT8_C( 0x5f ), UINT8_C( 0x58 ), UINT8_C( 0x4d ), UINT8_C( 0x4a ),
    UINT8_C( 0x43 ), UINT8_C( 0x44 ), UINT8_C( 0x19 ), UINT8_C( 0x1e ), UINT8_C( 0x17 ), UINT8_C( 0x10 ),
    UINT8_C( 0x05 ), UINT8_C( 0x02 ), UINT8_C( 0x0b ), UINT8_C( 0x0c ), UINT8_C( 0x21 ), UINT8_C( 0x26 ),
    UINT8_C( 0x2f ), UINT8_C( 0x28 ), UINT8_C( 0x3d ), UINT8_C( 0x3a ), UINT8_C( 0x33 ), UINT8_C( 0x34 ),
    UINT8_C( 0x4e ), UINT8_C( 0x49 ), UINT8_C( 0x40 ), UINT8_C( 0x47 ), UINT8_C( 0x52 ), UINT8_C( 0x55 ),
    UINT8_C( 0x5c ), UINT8_C( 0x5b ), UINT8_C( 0x76 ), UINT8_C( 0x71 ), UINT8_C( 0x78 ), UINT8_C( 0x7f ),
    UINT8_C( 0x6a ), UINT8_C( 0x6d ), UINT8_C( 0x64 ), UINT8_C( 0x63 ), UINT8_C( 0x3e ), UINT8_C( 0x39 ),
    UINT8_C( 0x30 ), UINT8_C( 0x37 ), UINT8_C( 0x22 ), UINT8_C( 0x25 ), UINT8_C( 0x2c ), UINT8_C( 0x2b ),
    UINT8_C( 0x06 ), UINT8_C( 0x01 ), UINT8_C( 0x08 ), UINT8_C( 0x0f ), UINT8_C( 0x1a ), UINT8_C( 0x1d ),
    UINT8_C( 0x14 ), UINT8_C( 0x13 ), UINT8_C( 0xae ), UINT8_C( 0xa9 ), UINT8_C( 0xa0 ), UINT8_C( 0xa7 ),
    UINT8_C( 0xb2 ), UINT8_C( 0xb5 ), UINT8_C( 0xbc ), UINT8_C( 0xbb ), UINT8_C( 0x96 ), UINT8_C( 0x91 ),
    UINT8_C( 0x98 ), UINT8_C( 0x9f ), UINT8_C( 0x8a ), UINT8_C( 0x8d ), UINT8_C( 0x84 ), UINT8_C( 0x83 ),
    UINT8_C( 0xde ), UINT8_C( 0xd9 ), UINT8_C( 0xd0 ), UINT8_C( 0xd7 ), UINT8_C( 0xc2 ), UINT8_C( 0xc5 ),
    UINT8_C( 0xcc ), UINT8_C( 0xcb ), UINT8_C( 0xe6 ), UINT8_C( 0xe1 ), UINT8_C( 0xe8 ), UINT8_C( 0xef ),
    UINT8_C( 0xfa ), UINT8_C( 0xfd ), UINT8_C( 0xf4 ), UINT8_C( 0xf3 )
};

void xrn_load_sub_type( uint8_t subtype, uint8_t * crc_bytes, int *nbits
 );

void xrn_dump_sub_type( uint8_t * subtype, uint8_t crc_bytes, int nbits
 );

typedef struct cksumnum_t {
    uint8_t num[32];
} cksumnum_t;

typedef struct header_block_t {
    uint32_t magic_number;
    uint32_t tag;
    uint64_t offset;
    uint64_t length;
    uint64_t total_length;
    uint8_t crc8;
} header_block_t;

typedef struct steg_header_block_t {
    header_block_t a;
    header_block_t b;
} steg_header_block_t;

void print_header_block( header_block_t header
 );

uint8_t cksumnum_buff_crc8( uint8_t buff_index, cksumnum_t * chsumnum_buff
 );

int get_random_bytes( uint8_t * buffer, uint32_t len
 );

#endif
